﻿Imports System.IO
Imports SharpCompress.Common
Imports SharpCompress.Writer
Imports System.Threading
Imports System.Net
Imports SACommon
Public Class Utils
#Region "Enums"
    Enum AppImageIndex
        NewApp = 0
        Defined = 1
        Uploaded = 2
    End Enum
    Enum VersionImageIndex
        NewApp = 0
        Defined = 1
        Uploaded = 2
        Retired = 3
    End Enum
    Enum FTPImageIndex
        Success = 0
        DelError = 1
    End Enum
#End Region
    Shared Function ReturnGroupIndicator(ReferenceFile As Boolean, Extension As String) As String
        If ReferenceFile = True Then Return "ref"
        If Extension = "exe" Or Extension = "dll" Then Return Extension
        Return "oth"
    End Function
    Shared Function DoDataUpload(ArchiveFileName As String, DestinationFilename As String) As Boolean
        'Microsoft inbuild FTP Upload
        'Try
        '    My.Computer.Network.UploadFile(ArchiveFileName, DestinationFilename, gApp.FTPUsername, gApp.FTPPassword, True, 1000000)
        '    Return True
        'Catch
        '    Return False
        'End Try
        'Webrequest method
        Try
            Dim ftp As FtpWebRequest = DirectCast(FtpWebRequest.Create(DestinationFilename), FtpWebRequest)
            ftp.Credentials = New NetworkCredential(gUDApp.SelectedApp.FTPUsername, gUDApp.SelectedApp.FTPPassword)

            ftp.KeepAlive = True
            ftp.UseBinary = True
            ftp.Method = WebRequestMethods.Ftp.UploadFile

            Dim fs As FileStream = File.OpenRead(ArchiveFileName)
            Dim buffer As Byte() = New Byte(fs.Length - 1) {}
            fs.Read(buffer, 0, buffer.Length)
            fs.Close()

            Dim ftpstream As Stream = ftp.GetRequestStream()
            ftpstream.Write(buffer, 0, buffer.Length)
            ftpstream.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    Shared Function ArchiveFile(OutputCompressedFilename As String, isLZMA As Boolean) As String
        'Shared Function ArchiveFile(Filename As String, InputUncompressedFilename As String, OutputCompressedFilename As String) As String
        Using stream As Stream = File.OpenWrite(OutputCompressedFilename)
            Dim cInfo As New CompressionInfo

            If isLZMA = True Then
                cInfo.Type = CompressionType.LZMA
            Else
                cInfo.Type = CompressionType.Deflate
            End If

            Using writer = WriterFactory.Open(stream, ArchiveType.Zip, cInfo)
                writer.WriteAll(gApp.TempDirectory, "*", SearchOption.AllDirectories)
            End Using
            Thread.Sleep(1000)
            'stream.Dispose()
        End Using
        Do
            Application.DoEvents()
        Loop Until File.Exists(OutputCompressedFilename) = True
        Thread.Sleep(1000)
        Return Security.GenerateFileMD5(OutputCompressedFilename)
    End Function
    Shared Sub StopAllUpdates()
        Dim verFile As New UpdateCore.UpdateVersion() With {.Allow = False,
                                                            .CoreVersion = "",
                                                            .CoreIsBeta = False,
                                                            .CoreType = "",
                                                            .InstallerVersion = "",
                                                            .InstallerMD5 = "",
                                                            .HelpVersion = "",
                                                            .HelpMD5 = ""}
        verFile.WriteVersionFile(gApp.UpdateVersionFile)
    End Sub
End Class
